package com.example.demo.controller;

import com.example.demo.entity.Result;
import com.example.demo.entity.req.LawsListsReq;
import com.example.demo.service.LawService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.example.demo.entity.req.LawsDetailReq;
import com.example.demo.entity.req.addLawReq;
import com.example.demo.entity.req.updateLawContentReq;
import com.example.demo.entity.req.updateLawInfoReq;
import com.example.demo.entity.req.deleteLawsReq;

import java.util.List;

@Slf4j
@RestController
public class LawController {
    @Autowired
    private LawService lawService;

    // 将旧版的法规数据读取出来处理
    @GetMapping("/oldToNew")
    public Result oldToNew(String TableName) {
        try {
            log.info("开始处理表: {}", TableName);
            int result = lawService.oldToNew(TableName);
            log.info("处理结果: {}", result);

            if (result == 0) {
                return new Result().error("保存失败");
            } else {
                return new Result().success();
            }
        } catch (Exception e) {
            log.error("服务器错误, 表名: {}, 错误详情: {}", TableName, e.getMessage(), e);
            return new Result().error("服务器错误: " + e.getMessage());
        }
    }

    // 获取法规列表
    @PostMapping("/getLawsLists")
    public Result getLawsLists(@RequestBody LawsListsReq req) {
        log.info(String.valueOf(req));
        try {
            return new Result().success(lawService.getLawsLists(req));
        } catch (Exception e) {
            return new Result().error("服务器错误");
        }
    }

    // 获取法规详情
    @PostMapping("/getLawDetail")
    public Result getLawsDetail(@RequestBody LawsDetailReq req) {
        log.info("获取法规详情: " + String.valueOf(req));

        if (req.getId() == null || req.getId().equals("")) {
            return new Result().error("id不能为空");
        }
        try {

            return new Result().success(lawService.getLawDetail(req));
        } catch (Exception e) {
            return new Result().error("服务器错误");
        }
    }

    // 更新文章内容和目录
    @PostMapping("/updateLawContent")
    public Result updateLawContent(@RequestBody updateLawContentReq req) {
        log.info("更新文章内容和目录: " + String.valueOf(req));

        if (req.getId() == null || req.getId().equals("")) {
            return new Result().error("id不能为空");
        }
        try {
            int result = lawService.updateLawContent(req);
            if (result == 0) {
                return new Result().error("更新失败");
            }
            return new Result().success();
        } catch (Exception e) {
            return new Result().error("服务器错误");
        }
    }

    // 更新文章信息
    @PostMapping("/updateLawInfo")
    public Result updateLawInfo(@RequestBody updateLawInfoReq req) {

        if (req.getId() == null || req.getId().equals("")) {
            return new Result().error("id不能为空");
        }
        try {
            int result = lawService.updateLawInfo(req);
            if (result == 0) {
                return new Result().error("更新失败");
            }
            return new Result().success();
        } catch (Exception e) {
            return new Result().error("服务器错误");
        }
    }

    // 上传图片
    @PostMapping("/uploadImage/{id}")
    public Result uploadImage(@RequestParam("image") MultipartFile image,
            @PathVariable("id") String id) {
        log.info("上传图片: id={}, fileName={}", id, image.getOriginalFilename());

        if (image.isEmpty()) {
            return new Result().error("图片文件不能为空");
        }

        if (id == null || id.trim().isEmpty()) {
            return new Result().error("id不能为空");
        }

        try {
            String result = lawService.uploadImage(image, id);
            return new Result().success(result);
        } catch (Exception e) {
            log.error("上传图片失败", e);
            return new Result().error("服务器错误");
        }
    }

    // 新增法规
    @PostMapping("/addLaw")
    public Result addLaw(@RequestBody addLawReq req) {
        log.info("新增法规请求: {}", req);

        if (req.getTitle() == null || req.getTitle().trim().isEmpty()) {
            return new Result().error("标题不能为空");
        }
        if (req.getCode() == null || req.getCode().trim().isEmpty()) {
            return new Result().error("法规代码不能为空");
        }

        try {
            String lawId = lawService.addLaw(req);
            if (lawId != null) {
                log.info("新增法规成功，返回ID: {}", lawId);
                return new Result().success(lawId);
            } else {
                return new Result().error("新增法规失败");
            }
        } catch (Exception e) {
            log.error("新增法规时发生异常", e);
            return new Result().error("服务器错误: " + e.getMessage());
        }
    }

    // 删除法规列表
    @PostMapping("/deleteLaws")
    public Result deleteLaws(@RequestBody deleteLawsReq req) {
        log.info("删除法规请求: {}", req);

        if (req.getIds() == null || req.getIds().isEmpty()) {
            return new Result().error("请选择要删除的法规");
        }

        try {
            int result = lawService.deleteLaws(req.getIds());
            if (result == 0) {
                return new Result().error("删除失败");
            }
            log.info("成功删除 {} 条法规", req.getIds().size());
            return new Result().success("删除成功");
        } catch (Exception e) {
            log.error("删除法规时发生异常", e);
            return new Result().error("服务器错误: " + e.getMessage());
        }
    }

    // 获取所有的标签种类
    @GetMapping("/getAllTags")
    public Result getAllTags() {
        log.info("获取所有标签请求");
        try {
            List<String> tags = lawService.getAllTags();
            return new Result().success(tags);
        } catch (Exception e) {
            log.error("获取标签时发生异常", e);
            return new Result().error("服务器错误: " + e.getMessage());
        }
    }

}